package com.velvetantler.weather.view.detail

import android.os.Handler
import android.os.Looper
import com.qweather.sdk.Callback
import com.qweather.sdk.QWeather
import com.qweather.sdk.parameter.weather.WeatherParameter
import com.qweather.sdk.response.error.ErrorResponse
import com.qweather.sdk.response.weather.WeatherDailyResponse
import com.velvetantler.weather.base.BaseInteractor
import com.velvetantler.weather.utils.WeatherUtils

class WeatherDetailModel : BaseInteractor<WeatherDetailView>() {

    fun requestWeather3dayData(
        longitude: String,
        latitude: String,
        onSucceed: (WeatherDailyResponse) -> Unit,
        onFailed: (String) -> Unit
    ) {
        // 格式化经纬度
        val longitudeFormat = WeatherUtils.formatLatLng(longitude)
        val latitudeFormat = WeatherUtils.formatLatLng(latitude)
        val location = "$longitudeFormat,$latitudeFormat"
        // 发送请求
        QWeather.instance.weather3d(
            WeatherParameter(location),
            object : Callback<WeatherDailyResponse> {
                override fun onSuccess(res: WeatherDailyResponse) {
                    Handler(Looper.getMainLooper()).post {
                        onSucceed(res)
                    }
                }

                override fun onFailure(p0: ErrorResponse) {
                    onFailed(p0.toString())
                }

                override fun onException(p0: Throwable) {
                    onFailed(p0.toString())
                }
            })
    }
}